package com.ty.zls.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIViewHelper;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;
import com.ty.zls.R;
import com.ty.zls.activity.CheckWaterActivity;
import com.ty.zls.activity.LoginActivity;
import com.ty.zls.activity.PresortActivity;
import com.ty.zls.activity.SearchActivity;
import com.ty.zls.util.StrogeUtil;

public class OtherFragment extends RelativeLayout implements View.OnClickListener {

    private Context context;

    public OtherFragment(Context context){
        super(context);
        this.context = context;
        initOther();
    }


    int payReRrintId = QMUIViewHelper.generateViewId();
    int waterReRrintId = QMUIViewHelper.generateViewId();
    int waterQueryId = QMUIViewHelper.generateViewId();
    int presortQueryId = QMUIViewHelper.generateViewId();
    int logoutId = QMUIViewHelper.generateViewId();
    int checkWaterId = QMUIViewHelper.generateViewId();

    private void initOther() {
        LayoutInflater.from(context).inflate(R.layout.other_fragment, this);
        QMUIGroupListView qg_other_list = this.findViewById(R.id.qg_other_list);

        QMUICommonListItemView waterQuery = qg_other_list.createItemView(
                ContextCompat.getDrawable(context, R.mipmap.lasttime),
                "抄表查询",
                null,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        waterQuery.setId(waterQueryId);

        QMUICommonListItemView presortQuery = qg_other_list.createItemView(
                ContextCompat.getDrawable(context, R.mipmap.presort),
                "置后用户",
                null,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        presortQuery.setId(presortQueryId);

        QMUICommonListItemView waterReRrint = qg_other_list.createItemView(
                ContextCompat.getDrawable(context, R.mipmap.reprint),
                "打印账单",
                null,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        waterReRrint.setId(waterReRrintId);

        QMUICommonListItemView payReRrint = qg_other_list.createItemView(
                ContextCompat.getDrawable(context, R.mipmap.print),
                "缴费代收",
                null,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        payReRrint.setId(payReRrintId);

        QMUICommonListItemView checkWater = qg_other_list.createItemView(
                ContextCompat.getDrawable(context, R.mipmap.print),
                "抄表复核",
                null,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        checkWater.setId(checkWaterId);

        QMUICommonListItemView logout = qg_other_list.createItemView(
                ContextCompat.getDrawable(context, R.mipmap.reset),
                "登出",
                null,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        logout.setId(logoutId);

        int size = QMUIDisplayHelper.dp2px(context, 20);
        QMUIGroupListView.newSection(context)
                .setTitle(null)
                .setDescription(null)
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addItemView(waterReRrint, this)
                .addItemView(payReRrint, this)
                .addItemView(waterQuery, this)
                .addItemView(presortQuery, this)
                .addItemView(checkWater, this)
                .addItemView(logout, this)
                .addTo(qg_other_list);
    }

    @Override
    public void onClick(View v) {

        if (v.getId() == waterReRrintId) {
            //票据重打
            Intent intent = new Intent(context, SearchActivity.class);
            intent.putExtra("flag", 0);
            context.startActivity(intent);
        } else if (v.getId() == waterQueryId) {
            Intent intent = new Intent(context, SearchActivity.class);
            intent.putExtra("flag", 1);
            context.startActivity(intent);
        } else if (v.getId() == presortQueryId) {
            Intent intent = new Intent(context, PresortActivity.class);
            context.startActivity(intent);
        } else if (v.getId() == payReRrintId) {
            Intent intent = new Intent(context, SearchActivity.class);
            intent.putExtra("flag", 2);
            context.startActivity(intent);
        } else if (v.getId() == logoutId) {
            new QMUIDialog.MessageDialogBuilder(context)
                    .setTitle("注销")
                    .setMessage("确定登出吗？")
                    .addAction("取消", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            dialog.dismiss();
                        }
                    })
                    .addAction("确定", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            StrogeUtil.rmToken(context);
                            context.startActivity(new Intent(context, LoginActivity.class));
                            ((Activity)context).finish();
                        }
                    })
                    .create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show();

        } else if (v.getId() == checkWaterId){
            context.startActivity(new Intent(context, CheckWaterActivity.class));
        }
    }
}
